package com.gl.library.widget.calendar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;

import com.gl.library.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 项目名称：Calendar
 */
public class CalendarView extends View {
    private static final String TAG = "CalendarView";

    private final String[] WEEK_STR = new String[]{"日", "一", "二", "三", "四", "五", "六"};


    @ColorInt
    private int mTextColorMonth = Color.BLACK;        //标题字体颜色
    @ColorInt
    private int mTextColorWeek = 0xa3a3a3;            //星期字体颜色
    @ColorInt
    private int mTextColorDay = Color.BLACK;          //日期字体颜色
    @ColorInt
    private int mSelectTextColor = Color.TRANSPARENT; //选中日期字体颜色
    @ColorInt
    private int mSelectBg = Color.TRANSPARENT;        //选中日期背景
    @ColorInt
    private int mOtherTextColor = Color.RED;       //当天日期背景

    private float mTextSizeMonth;             //标题字体大小
    private float mTextSizeWeek;              //星期字体大小
    private float mTextSizeDay;               //日期字体大小
    private Bitmap mMonthRowL;            //月份箭头
    private Bitmap mMonthRowR;            //月份箭头
    private float mMonthRowSpace;           //箭头间距
    private float mPaddingTop;              //标题月份下间隔
    private float mMarginTitle;

    private float mSelectRadius;            //选中日期背景半径
    private float mLineSpace;               //日期行间距
    private float mDotRadius;         //小圆点半径

    private String mMonthFormat;        //title显示的月份格式

    private Paint mPaint;
    private Paint mBgPaint;
    private Paint mDotPaint;
    private Paint mBitmapPaint;

    private SimpleDateFormat mDateFormat;

    private Calendar mCalendar;     //当前月份的日历
    private boolean mIsCurrentMonth; //展示的月份是否是当前月
    private int mCurrentDay;        //当前日期



    private int mSelectDay;          //选中的日期
    private int mLastSelectDay;      //上一次选中的日期（避免造成重复回调请求）


    private int mDayOfMonth;         //月份天数
    private int mFirstIndex;         //当月第一天位置索引
    private int mLineNum;            //日期行数
    private int mFirstLineNum;       //第一行、最后一行能展示多少日期
    private int mLastLineNum;

    private float mTitleHeight;
    private float mWeekHeight;
    private float mDayHeight;
    private float mDotHeight;
    private float mOneHeight;

    private int mColumnWidth;       //每列宽度


    private OnClickListener mOnClickListener;

    /**
     * 绘制月份
     */
    private int mRowLStart, mRowRStart, mRowWidth;

    //焦点坐标
    private PointF mFocusPoint = new PointF();

    //控制事件是否响应
    private boolean mResponseWhenEnd = false;

    private SparseArray<DotBean> mData = new SparseArray<>(31);


    public CalendarView(Context context) {
        this(context, null);
    }


    public CalendarView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public CalendarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context
                .obtainStyledAttributes(attrs, R.styleable.CalendarView, defStyleAttr, R.style.CalendarDefault);

        mTextColorMonth = ta.getColor(R.styleable.CalendarView_mTextColorMonth, Color.BLACK);
        mTextColorWeek = ta.getColor(R.styleable.CalendarView_mTextColorWeek, 0xa3a3a3);
        mTextColorDay = ta.getColor(R.styleable.CalendarView_mTextColorDay, Color.BLACK);
        mSelectTextColor = ta
                .getColor(R.styleable.CalendarView_mSelectTextColor, mTextColorDay);
        mSelectBg = ta.getColor(R.styleable.CalendarView_mSelectBg, Color.TRANSPARENT);
        mOtherTextColor = ta
                .getColor(R.styleable.CalendarView_mOtherTextColor, mTextColorDay);

        mTextSizeMonth = ta.getDimension(R.styleable.CalendarView_mTextSizeMonth, 45);
        mTextSizeWeek = ta.getDimension(R.styleable.CalendarView_mTextSizeWeek, 36);
        mTextSizeDay = ta.getDimension(R.styleable.CalendarView_mTextSizeDay, 39);

        mMonthRowL = BitmapFactory.decodeResource(getResources(), ta
                .getResourceId(R.styleable.CalendarView_mMonthRowL, R.mipmap.report_left_icon));
        mMonthRowR = BitmapFactory.decodeResource(getResources(), ta
                .getResourceId(R.styleable.CalendarView_mMonthRowR, R.mipmap.report_right_icon));

        mMonthRowSpace = ta.getDimension(R.styleable.CalendarView_mMonthRowSpace, 138);

        mSelectRadius = ta.getDimension(R.styleable.CalendarView_mSelectRadius, 0);
        mPaddingTop = ta.getDimension(R.styleable.CalendarView_mPaddingTop, 0);
        mMarginTitle = ta.getDimension(R.styleable.CalendarView_mMarginTitle, 0);
        mLineSpace = ta.getDimension(R.styleable.CalendarView_mLineSpace, 10);
        mDotRadius = ta.getDimension(R.styleable.CalendarView_mDotRadius, 10);

        mMonthFormat = ta.getString(R.styleable.CalendarView_mMonthFormat);

        ta.recycle();

        init();
    }


    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mDotPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        mDotHeight = mDotRadius * 2;//小红点高度

        mPaint.setFakeBoldText(true);
        //标题高度
        mPaint.setTextSize(mTextSizeMonth);
        if (getFontHeight(mPaint) > mMonthRowL.getHeight()) {
            mTitleHeight = getFontHeight(mPaint) + mMarginTitle + mPaddingTop;
        } else {
            mTitleHeight = mMonthRowL.getHeight() + mMarginTitle + mPaddingTop;
        }

        //星期高度
        mPaint.setFakeBoldText(false);
        mPaint.setTextSize(mTextSizeWeek);
        mWeekHeight = getFontHeight(mPaint);
        //日期高度
        mPaint.setTextSize(mTextSizeDay);
        mDayHeight = getFontHeight(mPaint);

        //每行高度 = 行间距 + 日期字体高度 + 小圆点间距 + 小圆点高度
        mOneHeight = mLineSpace + mDayHeight;

        setMonth(new Date());
    }


    /**
     * 设置月份
     */
    private void setMonth(Date month) {
        mCalendar = Calendar.getInstance();

        //获取当前年月日
        mCalendar.setTime(new Date());
        int currentYear = mCalendar.get(Calendar.YEAR);
        int currentMonth = mCalendar.get(Calendar.MONTH);
        mCurrentDay = mCalendar.get(Calendar.DAY_OF_MONTH);

        //设置的日历
        mCalendar.setTime(month);
        mCalendar.set(Calendar.DAY_OF_MONTH, 1);
        //判断是否为当月
        if (currentYear == mCalendar.get(Calendar.YEAR) &&
                currentMonth == mCalendar.get(Calendar.MONTH)) {
            mIsCurrentMonth = true;
            mSelectDay = mCurrentDay;//当月默认选中当前日
        } else {
            mIsCurrentMonth = false;
            mSelectDay = 0;
        }
        //Log.i(TAG, "设置月份：" + getMonthStr(month) + "   今天" + mCurrentDay + "号, 是否为当前月：" +
        //        mIsCurrentMonth);

        mDayOfMonth = mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        //第一行1号显示在什么位置（星期几）
        mFirstIndex = mCalendar.get(Calendar.DAY_OF_WEEK) - 1;
        mLineNum = 1;
        //第一行能展示的天数
        mFirstLineNum = 7 - mFirstIndex;
        mLastLineNum = 0;
        int surplus = mDayOfMonth - mFirstLineNum;
        while (surplus > 7) {
            mLineNum++;
            surplus -= 7;
        }
        if (surplus > 0) {
            mLineNum++;
            mLastLineNum = surplus;
        }
        //Log.i(TAG, getMonthStr(month) + "一共有" + mDayOfMonth + "天,第一天的索引是：" + mFirstIndex + "   有" +
        //        mLineNum + "行，第一行" + mFirstLineNum + "个，最后一行" + mLastLineNum + "个");
    }


    private String getMonthStr(Date month) {
        if (mDateFormat == null) {
            if (mMonthFormat == null) {
                mMonthFormat = "yyyy年MM月";
            }
            mDateFormat = new SimpleDateFormat(mMonthFormat, Locale.getDefault());
        }
        return mDateFormat.format(month);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //宽度 = 填充父窗体
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);   //获取宽的尺寸
        mColumnWidth = widthSize / 7;
        //高度 = 标题高度 + 星期高度 + 日期行数*每行高度
        float height = mTitleHeight + mWeekHeight + (6 * mOneHeight) + mLineSpace;
        //Log.i(TAG, "标题高度：" + mTitleHeight + " 星期高度：" + mWeekHeight + " 每行高度：" + mOneHeight + " 行数：" +
        //        mLineNum + "  \n控件高度：" + height);
        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), (int) height);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawMonth(canvas);
        drawWeek(canvas);
        drawDayAndDot(canvas);
    }


    private void drawMonth(Canvas canvas) {
        //背景

        //绘制月份
        mPaint.setFakeBoldText(true);
        mPaint.setTextSize(mTextSizeMonth);
        mPaint.setColor(mTextColorMonth);
        String monthStr = getMonthStr(mCalendar.getTime());
        float textLen = getFontLength(mPaint, monthStr);
        float textStart = (getWidth() - textLen) / 2;
        canvas.drawText(monthStr, textStart, mPaddingTop + mMonthRowL.getHeight() / 2 + getFontCenter(mPaint), mPaint);
//        canvas.drawText(monthStr, textStart, mMonthPadding, mPaint);
        //绘制左右箭头
        mRowWidth = mMonthRowL.getWidth();
        //float left, float top
        mRowLStart = (int) (textStart - mMonthRowSpace - mRowWidth);
        canvas.drawBitmap(mMonthRowL, mRowLStart, mPaddingTop, mBitmapPaint);
        mRowRStart = (int) (textStart + textLen + mMonthRowSpace);
        canvas.drawBitmap(mMonthRowR, mRowRStart, mPaddingTop, mBitmapPaint);
    }


    private void drawWeek(Canvas canvas) {
        //背景

        //绘制星期：七天
        mPaint.setFakeBoldText(false);
        mPaint.setTextSize(mTextSizeWeek);
        mPaint.setColor(mTextColorWeek);
        for (int i = 0; i < WEEK_STR.length; i++) {
            int len = (int) getFontLength(mPaint, WEEK_STR[i]);
            int x = i * mColumnWidth + (mColumnWidth - len) / 2;
            canvas.drawText(WEEK_STR[i], x, mTitleHeight + getFontLeading(mPaint), mPaint);
        }
    }


    private void drawDayAndDot(Canvas canvas) {
        //某行开始绘制的Y坐标，第一行开始的坐标为标题高度+星期部分高度
        float top = mTitleHeight + mWeekHeight;
        //行
        for (int line = 0; line < mLineNum; line++) {
            if (line == 0) {
                //第一行
                drawDayAndDot(canvas, top, mFirstLineNum, 0, mFirstIndex);
                drawFirst(canvas, top);
            } else if (line == mLineNum - 1) {
                //最后一行
                top += mOneHeight;
                drawDayAndDot(canvas, top, mLastLineNum, mFirstLineNum + (line - 1) * 7, 0);
                drawLast(canvas, top, mLineNum - 1);
            } else {
                //满行
                top += mOneHeight;
                drawDayAndDot(canvas, top, 7, mFirstLineNum + (line - 1) * 7, 0);
            }
        }

    }

    private void drawLast(Canvas canvas, float top, int mLineNum) {
        Log.e(TAG, "drawLast: " + mLastLineNum + "   " + mLineNum);
        float dayTextLeading = getFontLeading(mPaint);
        int day = 1;
        for (int line = mLineNum; line < 6; line++) {
            if (line == mLineNum) {
                //第一行
                for (int i = mLastLineNum; i < 7; i++) {
                    int left = (0 + i) * mColumnWidth;
                    mPaint.setColor(mOtherTextColor);
                    mPaint.setTextSize(mTextSizeDay);

                    int len = (int) getFontLength(mPaint, day + "");
                    int x = left + (mColumnWidth - len) / 2;
                    float y = top + mLineSpace + dayTextLeading;


                    canvas.drawText(day + "", x, y, mPaint);
                    day++;

                }
                top += mOneHeight;
            } else {
                for (int i = 0; i < 7; i++) {
                    int left = (0 + i) * mColumnWidth;
                    mPaint.setColor(mOtherTextColor);
                    mPaint.setTextSize(mTextSizeDay);

                    int len = (int) getFontLength(mPaint, day + "");
                    int x = left + (mColumnWidth - len) / 2;
                    float y = top + mLineSpace + dayTextLeading;


                    canvas.drawText(day + "", x, y, mPaint);
                    day++;

                }
            }
        }


    }

    private void drawFirst(Canvas canvas, float top) {
        if (mFirstIndex > 0) {
            Calendar c = Calendar.getInstance();
            c.setTime(mCalendar.getTime());
            c.set(Calendar.DAY_OF_MONTH, 1);
            c.set(Calendar.DAY_OF_WEEK, 1);
            int fff = c.get(Calendar.DAY_OF_MONTH);
            Log.e(TAG, "drawDayAndDot: " + fff);
            float dayTextLeading = getFontLeading(mPaint);
            for (int i = 0; i < mFirstIndex; i++) {
                int left = (0 + i) * mColumnWidth;
                int day = (fff + i);
                mPaint.setColor(mOtherTextColor);
                mPaint.setTextSize(mTextSizeDay);

                int len = (int) getFontLength(mPaint, day + "");
                int x = left + (mColumnWidth - len) / 2;
                float y = top + mLineSpace + dayTextLeading;


                canvas.drawText(day + "", x, y, mPaint);

            }

        }
    }


    /**
     * 绘制某一行的日期
     *
     * @param top        顶部坐标
     * @param count      此行需要绘制的日期数量（不一定都是7天）
     * @param overDay    已经绘制过的日期，从overDay+1开始绘制
     * @param startIndex 此行第一个日期的星期索引
     */
    private void drawDayAndDot(Canvas canvas, float top, int count, int overDay, int startIndex) {
        //Log.i(TAG, "总共" + mDayOfMonth + "天  有" + mLineNum + "行" + "  已经画了" + overDay + "天,下面绘制：" +
        //        count + "天");
        //背景
        float topDot = top + mLineSpace + mDayHeight;
        mPaint.setTextSize(mTextSizeDay);
        float dayTextLeading = getFontLeading(mPaint);
        //        Log.v(TAG, "当前日期："+currentDay+"   选择日期："+mSelectDay+"  是否为当前月："+mIsCurrentMonth);
        for (int i = 0; i < count; i++) {
            int left = (startIndex + i) * mColumnWidth;
            int day = (overDay + i + 1);

            mPaint.setTextSize(mTextSizeDay);

            int len = (int) getFontLength(mPaint, day + "");
            int x = left + (mColumnWidth - len) / 2;
            float y = top + mLineSpace + dayTextLeading;
            DotBean dotBean = mData.get(day);
            //选中的日期，如果是本月，选中日期正好是当天日期，下面的背景会覆盖上面绘制的虚线背景
            if (mSelectDay == day) {
                //选中的日期字体白色，橙色背景
                mPaint.setColor(mSelectTextColor);
                mBgPaint.setColor(mSelectBg);
                //绘制橙色圆背景，参数一是中心点的x轴，参数二是中心点的y轴，参数三是半径，参数四是paint对象；
                canvas.drawCircle(
                        left + mColumnWidth / 2,
                        top + mLineSpace + mDayHeight / 2, mSelectRadius, mBgPaint);


            } else {

                mPaint.setColor(mTextColorDay);
            }

            canvas.drawText(day + "", x, y, mPaint);

            //绘制小圆点
            Log.e(TAG, "drawDayAndDotttt: " + mDayHeight + "   " + dayTextLeading);
            if (dotBean != null && dotBean.isShowDot()) {
                mDotPaint.setColor(Color.parseColor("#3D7EFF"));
                x += len;
                canvas.drawCircle(x + mDotRadius + 7, top + mLineSpace + mDotRadius + (mDayHeight - dayTextLeading) / 2, mDotRadius, mDotPaint);
            }
        }
    }


    /****************************事件处理↓↓↓↓↓↓↓****************************/

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction() & MotionEvent.ACTION_MASK;
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mFocusPoint.set(event.getX(), event.getY());
                touchFocusMove(mFocusPoint, true);
                break;
            case MotionEvent.ACTION_MOVE:
                //mFocusPoint.set(event.getX(), event.getY());
                //touchFocusMove(mFocusPoint, false);
                break;
            case MotionEvent.ACTION_OUTSIDE:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
//                mFocusPoint.set(event.getX(), event.getY());
//                touchFocusMove(mFocusPoint, true);
                break;
        }
        return true;
    }


    /**
     * 焦点滑动
     */
    public void touchFocusMove(final PointF point, boolean eventEnd) {
        //Log.e(TAG, "点击坐标：(" + point.x + " ，" + point.y + "),事件是否结束：" + eventEnd);
        //标题和星期只有在事件结束后才响应
        if (point.y <= mTitleHeight) {
            //事件在标题上
            if (eventEnd && mOnClickListener != null) {
                if (point.x >= (mRowLStart - mRowWidth * 2) &&
                        point.x < (mRowLStart + mRowWidth * 3)) {
                    //Log.w(TAG, "点击左箭头");
                    mOnClickListener.onLeftRowClick();
                } else if (point.x > (mRowRStart - mRowWidth * 2) &&
                        point.x < (mRowRStart + mRowWidth * 3)) {
                    //Log.w(TAG, "点击右箭头");
                    mOnClickListener.onRightRowClick();
                } else if (point.x > mRowLStart && point.x < mRowRStart) {
                    Date time = mCalendar.getTime();
                    mOnClickListener.onTitleClick(getMonthStr(time), time);
                }
            }
        } else if (point.y <= (mTitleHeight + mWeekHeight)) {

            //事件在星期部分
            if (eventEnd && mOnClickListener != null) {
                //根据X坐标找到具体的焦点日期
                int xIndex = (int) point.x / mColumnWidth;
                //Log.e(TAG, "列宽：" + mColumnWidth + "  x坐标余数：" + (point.x / mColumnWidth));
                if ((point.x / mColumnWidth - xIndex) > 0) {
                    xIndex += 1;
                }
                mOnClickListener.onWeekClick(xIndex - 1, WEEK_STR[xIndex - 1]);
            }
        } else {
            //日期部分按下和滑动时重绘，只有在事件结束后才响应
            touchDay(point, eventEnd);
        }
    }


    /**
     * 事件点在 日期区域 范围内
     */
    private void touchDay(final PointF point, boolean eventEnd) {
        //根据Y坐标找到焦点行
        boolean availability = false;  //事件是否有效
        //日期部分
        float top = mTitleHeight + mWeekHeight + mOneHeight;
        int focusLine = 1;
        while (focusLine <= mLineNum) {
            if (top >= point.y) {
                availability = true;
                break;
            }
            top += mOneHeight;
            focusLine++;
        }
        if (availability) {
            //根据X坐标找到具体的焦点日期
            int xIndex = (int) point.x / mColumnWidth;
            if ((point.x / mColumnWidth - xIndex) > 0) {
                xIndex += 1;
            }
            //            Log.e(TAG, "列宽："+mColumnWidth+"  x坐标余数："+(point.x / mColumnWidth));
            if (xIndex <= 0) {
                xIndex = 1;   //避免调到上一行最后一个日期
            }
            if (xIndex > 7) {
                xIndex = 7;   //避免调到下一行第一个日期
            }
            //            Log.e(TAG, "事件在日期部分，第"+focusLine+"/"+mLineNum+"行, "+xIndex+"列");
            if (focusLine == 1) {
                //第一行
                if (xIndex <= mFirstIndex) {
                    //Log.e(TAG, "点到开始空位了");
                    clickEmpty();
                    //setSelectedDay(mSelectDay, true);
                } else {
                    setSelectedDay(xIndex - mFirstIndex, eventEnd);
                }
            } else if (focusLine == mLineNum) {
                //最后一行
                if (xIndex > mLastLineNum) {
                    //Log.e(TAG, "点到结尾空位了");
                    clickEmpty();
                    //setSelectedDay(mSelectDay, true);
                } else {
                    setSelectedDay(mFirstLineNum + (focusLine - 2) * 7 + xIndex, eventEnd);
                }
            } else {
                setSelectedDay(mFirstLineNum + (focusLine - 2) * 7 + xIndex, eventEnd);
            }
        } else {
            //超出日期区域后，视为事件结束，不响应最后一个选择日期的回调
            //Log.e(TAG, "点击超出日期区域了");
            clickEmpty();
            //setSelectedDay(mSelectDay, true);
        }
    }


    private void clickEmpty() {
        mResponseWhenEnd = false;

    }


    /**
     * 设置选中的日期
     */
    private void setSelectedDay(int day, boolean eventEnd) {
        //Log.w(TAG, "选中：" + day + "  事件是否结束" + eventEnd);
        mSelectDay = day;
        invalidate();
        if (mOnClickListener != null && eventEnd  &&
                mLastSelectDay != mSelectDay) {
            mLastSelectDay = mSelectDay;
            mOnClickListener.onDayClick(mSelectDay,
                    getMonthStr(mCalendar.getTime()) + mSelectDay + "日", mData.get(mSelectDay));
        }
//        mResponseWhenEnd = !eventEnd;
    }


    /****************************事件处理↑↑↑↑↑↑↑****************************/

    public void setData(Date month, List<? extends DotBean> list) {
        setMonth(month);
        if (list != null && list.size() > 0) {
            mData.clear();
            for (DotBean finish : list) {
                mData.put(finish.getDay(), finish);
            }
        }

        invalidate();
    }
    public int getmSelectDay() {
        return mSelectDay;
    }

    public void setData(List<? extends DotBean> list) {
        if (list != null && list.size() > 0) {
            mData.clear();
            for (DotBean finish : list) {
                mData.put(finish.getDay(), finish);
            }
            mOnClickListener.onHaveData(mSelectDay,getMonthStr(mCalendar.getTime()) + mSelectDay + "日");
        }
        invalidate();

    }

    /**
     * 获取当月的天数
     */
    public int getmDayOfMonth() {
        return mDayOfMonth;
    }
    /**
     * 获取当前的月份
     */
    public String getMonth() {
        return new SimpleDateFormat("M", Locale.getDefault()).format(mCalendar.getTime());
    }
    /**
     * 获取当前的年
     */
    public String getYear() {
        return new SimpleDateFormat("yyyy", Locale.getDefault()).format(mCalendar.getTime());
    }

    public String getDay() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String time = simpleDateFormat.format(mCalendar.getTime());
        return time;
    }
    public String getMouth() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-", Locale.getDefault());
        String time = simpleDateFormat.format(mCalendar.getTime());
        return time;
    }

    /**
     * 月份增减
     */
    public void monthChange(int change) {
        mCalendar.add(Calendar.MONTH, change);
        setMonth(mCalendar.getTime());
        mData.clear();
        invalidate();
    }


    /**
     * 隐藏弹窗
     */


    /**
     * @return 返回指定笔和指定字符串的长度
     */
    public  float getFontLength(Paint paint, String str) {
        return paint.measureText(str);
    }


    /**
     * @return 返回指定笔的文字高度
     */
    public  float getFontHeight(Paint paint) {
        Paint.FontMetrics fm = paint.getFontMetrics();
        return fm.descent - fm.ascent;
    }


    /**
     * @return 返回指定笔离文字顶部的基准距离
     */
    public  float getFontLeading(Paint paint) {
        Paint.FontMetrics fm = paint.getFontMetrics();
        return fm.leading - fm.ascent;
    }

    /**
     * @return 返回指定笔离文字顶部的基准距离
     */
    public  float getFontCenter(Paint paint) {
        Paint.FontMetrics fm = paint.getFontMetrics();
        return (fm.descent - fm.ascent) / 2 - fm.descent;
    }


    public void setOnClickListener(OnClickListener listener) {
        this.mOnClickListener = listener;
    }


    public interface OnClickListener {

        void onLeftRowClick();

        void onRightRowClick();

        void onTitleClick(String monthStr, Date month);

        void onWeekClick(int weekIndex, String weekStr);

        void onDayClick(int day, String dayStr, DotBean finish);
        void onHaveData(int day, String dayStr);
    }

    public interface DotBean {
        boolean isShowDot();

        int getDay();

    }
}
